# -*- coding: utf-8 -*-#

# -------------------------------------------------------------------------------
# Name:         job51_utils
# Description:  提取51job页面的内容
# Author:       zhuchunxu
# Date:         2020/11/3
# -------------------------------------------------------------------------------
import datetime
import re
from logging import getLogger

from lxml import etree
from lxml.html import tostring

from spider_source.items import Job


class Ybrc1Utils:

    def __init__(self):
        self.logger = getLogger(__name__)

    def get_page_detail_links(self, response):
        """
        获取当前页所有的详情页链接
        """
        html = etree.HTML(response.text)
        job_li_list = html.xpath("//div[@class='qzListBox_v2 qz']/ul/li")

        cur_page_detail_link = []
        for li in job_li_list:
            href = li.xpath("./div[1]/div[1]/a/@href")[0]
            href = "http://www.yibin.ccoo.cn" + href
            cur_page_detail_link.append(href)
        print(cur_page_detail_link)
        return cur_page_detail_link

    def get_page_detail_info(self, response):
        html = etree.HTML(response.text)
        job = Job()
        page_main=html.xpath("//div[@class='fc-wrap']")[0]
        print(page_main)
        try:
            # 获取岗位名称
            job["title"] = page_main.xpath("//div[@class='zMain-titBox']/h2/text()")[0].strip()
            print("========" + job["title"])
        except Exception as e:
            self.logger.error('提取岗位名称错误')

        # 职位链接
        job["job_url"] = response.url
        job["from_site"] = "yibin.ccoo.cn"

        # 公司名称
        try:
            job["job_company"] = page_main.xpath("//div[@class='conBox zComBox']/p/text()")[0].strip()
            print("========" + job["job_company"])
        except Exception as e:
            self.logger.error('提取公司名称错误')

        # 职位类别
        job["category"] = page_main.xpath("//div[@class='zInfoBox']/ul/li[1]/span[2]/text()")[0].strip()
        print("========" + job["category"])

        if "兼职" in job["category"]:
            job["catalog"] = "兼职"
        else:
            job["catalog"] = "全职"

        # 职位描述
        try:
            job["job_desc"] = page_main.xpath("//div[@class='fc-editBox']/p")[0].xpath('string(.)').strip()
            print("========" + job["job_desc"])
        except Exception as e:
            self.logger.error('提取岗位内容错误:url:{},field:{}'.format(response.url, "job_desc"))

        # 获取区域
        address = page_main.xpath("//div[@class='zInfoBox']/ul/li[6]/span[2]/text()")[0].strip()
        print("========" + address)

        if len(address) > 0:
            try:
                if "市" in address:
                    job["city"] = address.split("市")[0].strip() + "市"
                    job["area"] = address.split("市")[1].split("区")[0].strip() + "区"
                else:
                    job["city"] = address.strip()
                    job["area"] = address.strip()
            except Exception as e:
                self.logger.error('提取城市、区域等内容错误')
        print("========" + job["city"] + job["area"])
        # 获取学历
        job["education"] = page_main.xpath("//div[@class='zInfoBox']/ul/li[2]/span[2]/text()")[0].strip()
        print("========" + job["education"])

        # 获取经验
        job["experience"] = page_main.xpath("//div[@class='zInfoBox']/ul/li[4]/span[2]/text()")[0].strip()
        print("========" + job["experience"])
        # 获取招聘人数
        job["recruit"] = page_main.xpath("//div[@class='zInfoBox']/ul/li[3]/span[2]/text()")[0].strip()
        print("========" + job["recruit"])
        # 获取日期
        date = page_main.xpath("//div[@class='tabs1 fl']/span[1]/text()")[0].strip()
        date = date.split("：")[1]
        print(date)

        # try:
        #     job["date"] = "{}-{}".format(datetime.datetime.now().year,date)
        # except Exception as e:
        #     self.logger.error('提取发布日期错误')

        try:
            salary = page_main.xpath("//div[@class='zInfoBox']/p[1]/text()")[0].strip()
            print("========" + salary)
            if len(salary) > 0:
                salary_range = re.findall(r"\d+\.?\d*", salary)
                print(salary_range)
                if len(salary_range) > 0:
                    job["salary_min"] = salary_range[0]
                if len(salary_range) > 1:
                    job["salary_max"] = salary_range[1]
            else:
                job["salary_min"] = "面谈"
                job["salary_max"] = "面谈"
        except Exception as e:
            self.logger.error('提取薪资错误')
        print(job)
        return job
